package com.example.goodog.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.goodog.MyApplication;
import com.example.goodog.R;
import com.example.goodog.customViews.DogImage;
import com.example.goodog.items.TimeItem;

public class TimeAdapter extends ArrayAdapter<TimeItem>{
	
	Context context;
	protected ArrayList<TimeItem> items;
	private int LayoutID;
	private long ParkID;
	private String TAG = " GetAppointmentsTask";
	
	public TimeAdapter(Context c, ArrayList<TimeItem> items, long ParkID) {
		super(c, R.layout.time_item);
		context = c;
		this.items = items;
		LayoutID = R.layout.time_item;
		this.ParkID = ParkID;
	}
	
	@Override
	public int getCount() {
		return items.size();
	}
	@Override
	public TimeItem getItem(int arg0) {
		return items.get(arg0);
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View List;
		if(convertView==null){
			List=new View(context);
			LayoutInflater mLayoutinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			List=mLayoutinflater.inflate(LayoutID, parent, false);
		}
		else{
			List = (View)convertView;
		}
		
		final TimeItem item = ((TimeItem)items.get(position));

		
		TextView DateTimeText = (TextView)List.findViewById(R.id.dateTimeTextView);
		DateTimeText.setText(item.date);
		LinearLayout FemaleLL = (LinearLayout)List.findViewById(R.id.female);
		LinearLayout MaleLL = (LinearLayout)List.findViewById(R.id.male);
		TextView MaleText = (TextView)List.findViewById(R.id.maleCount);
		TextView FemaleText = (TextView)List.findViewById(R.id.femaleCount);
		Log.i(TAG, "get view at posiotion "+position);
		
		if(item.female>0)
		{
			Log.i(TAG, "Female found");
			FemaleLL.setVisibility(View.VISIBLE);
			FemaleText.setText(item.female+" X");
		}
		else
		{
			FemaleLL.setVisibility(View.GONE);
			FemaleText.setText("# X");
		}
		
		if(item.male>0)
		{
			Log.i(TAG, "Male found");
			MaleLL.setVisibility(View.VISIBLE);
			MaleText.setText(item.male+" X");
		}
		else
		{
			MaleLL.setVisibility(View.GONE);
			MaleText.setText("# X");
		}
		
		
		Log.i(TAG, "Female visable: "+FemaleLL.getVisibility()+ FemaleText.getText().toString());
		Log.i(TAG, "Male visable: "+MaleLL.getVisibility()+ MaleText.getText().toString());
		
		ImageButton AddTimeimageButton = (ImageButton)List.findViewById(R.id.AddTimeimageButton);
		AddTimeimageButton.setFocusable(false);
		AddTimeimageButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//Prompt dog selector
				MyApplication.MakeAppointment(context, item.date, ParkID);
			}
		});
		
		//Add Dogs
		LinearLayout toolbar = (LinearLayout) List.findViewById(R.id.toolbar);
		toolbar.removeAllViews();

		if(item.Images != null)
		{
			for (byte[] imgbolb : item.Images)
			{
				DogImage img = new DogImage(context, imgbolb);
				toolbar.addView(img);
			}
		}
		
		((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -30;
		toolbar.setVisibility(View.GONE);

		
		return List;
	}	
	
}
